package org.minos.edit.controller
{
	import flash.events.Event;
	import org.aswing.BorderLayout;
	import org.aswing.FlowLayout;
	import org.aswing.JFrame;
	import org.aswing.WindowLayout;
	import org.ichow.utils.StringUtil;
	import org.minos.edit.EditFacate;
	import org.minos.edit.model.AssetsProxy;
	import org.minos.edit.model.vo.ItemVO;
	import org.minos.edit.view.components.CreateScenePane;
	import org.minos.edit.view.components.SceneTable;
	import org.minos.edit.view.TableMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class AddSceneCommand extends SimpleCommand
	{
		
		public function AddSceneCommand()
		{
		}
		
		override public function execute( notification : INotification ) : void
		{
			super.execute( notification );
			
			var table : SceneTable = facade.retrieveMediator( TableMediator.NAME ).getViewComponent() as SceneTable;
			var dialog : JFrame = new JFrame( null , "新建场景" , true );
			var count : int = table.getRowCount();
			var vo : ItemVO = new ItemVO();
			var pane : CreateScenePane = new CreateScenePane();
			dialog.setContentPane( pane );
			
			pane.getOkButton().addActionListener( onOk );
			pane.getCancelButton().addActionListener( onCancel );
			pane.getUrlBtn().addActionListener( __onUrlClick );
			pane.getChildrenBtn().addActionListener( __onChildrenClick );
			pane.getScoreBtn().addActionListener( __onScoreClick );
			pane.getTypeCombox().setSelectedIndex(0);
			
			//dialog
			dialog.pack();
			dialog.show();
			sendNotification( EditFacate.POSITION , dialog );
			
		// ----- event methods ----- //
			
			function __onUrlClick( e : Event ) : void
			{
				sendNotification( EditFacate.SELECT_FILE , { tf: pane.getUrlTf() } , AssetsProxy.SWF );
			}
			
			function __onChildrenClick( e : Event ) : void
			{
				sendNotification( EditFacate.SELECT_SCENE , { tf: pane.getChildrenTf() , vo: vo , type: "children" } );
			}
			
			function __onScoreClick( e : Event ) : void
			{
				sendNotification( EditFacate.SETTER_SCORE , { tf: pane.getScoreTf() , vo: vo , type: "score" } );
			}
			
			function onOk( e : Event ) : void
			{
				
				vo.key = StringUtil.randomString( 10 );
				vo.label = pane.getLabelTf().getText();
				vo.frame = int( pane.getFrameTf().getText() );
				vo.score = pane.getScoreTf().getText();
				vo.url = pane.getUrlTf().getText();
				vo.tooltip = pane.getTooltipTf().getText();
				vo.info = pane.getInfoTf().getText();
				vo.continueInfo = pane.getContinueInfoTa().getText();
				vo.endInfo = pane.getEndInfoTa().getText();
				vo.type = pane.getTypeCombox().getSelectedIndex();
				
				dialog.dispose();
				table.addScene( vo );
			}
			
			function onCancel( e : Event ) : void
			{
				dialog.dispose();
				sendNotification( EditFacate.SCENE_SELECTION_CHANGED , null );
			}
			
		}
	
	}

}